package classloader;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class MyCustomClassloader extends ClassLoader {
  @Override
  public Class<?> loadClass(String className) throws ClassNotFoundException {
      return findClass(className);
  }

  @Override
  public Class<?> findClass(String className) throws ClassNotFoundException {
      byte[] b = null;
      Class c = null;
      try {
          b = loadClassData(className);
          c = defineClass(className, b, 0, b.length);
      } catch (IOException e) {
          e.printStackTrace();
      }
      return c;
  }

  private byte[] loadClassData(String className) throws IOException {
      int size = className.length();
      byte buff[] = new byte[size];

      // Open the file
      FileInputStream fis = new FileInputStream("bin/" + className.replace('.', File.separatorChar) + ".class");
      fis.available();
      fis.read(buff);
      fis.close();

      return buff;
  }

}
